<?php

namespace Common\Model;

/**
 * VipUserInfoModel.class.php实例模型
 *
 * @author zhangzhen
 * @date 2017-2-16
 */
class VipUserInfoModel extends BaseModel {

    protected $trueTableName = 'tb_vip_user_info';

    /**
     * 返回会员设置数据
     * @modify zhangzhen 2017-02-15    获取掌柜的会员设置信息
     * @param integer $hotelId 客栈ID
     * @return type
     */
    public function getInfoByHotel($hotelId) {
        $option = [
            'hotelentity_id'=> $hotelId,
        ];
        $field = 'id,hotelentity_id,name,auto,levelrule,discountrule';
        $data = $this->field($field)->where($option)->select();

        return empty($data) ? array() : $data;
    }
    
    /**
     * 返回会员设置数据
     * @modify zhangzhen 2017-02-15    获取掌柜的会员设置信息
     * @param integer $hotelId 客栈ID
     * @return type
     */
    public function getInfoById($id) {
        $option = [
            'id'=> $id,
        ];
        $field = 'id,hotelentity_id,name,auto,level,levelrule,discount,discountrule';
        $data = $this->field($field)->where($option)->find();

        return empty($data) ? array() : $data;
    }
    /**
     * 返回会员设置数据
     * @modify zhangzhen 2017-02-15    获取掌柜的会员设置信息
     * @param integer $hotelId 客栈ID
     * @param integer $name    等级名称
     * @return type
     */
    public function getInfoByHotelName($hotelId,$name) {
        $option = [
            'hotelentity_id'=> $hotelId,
            'name'=> $name,
        ];
        $field = 'id,hotelentity_id,name,auto,level,levelrule,discount,discountrule';
        $data = $this->field($field)->where($option)->find();

        return empty($data) ? array() : $data;
    }

    /**
     * 返回会员设置数据
     * @modify zhangzhen 2017-02-15    获取掌柜的会员设置信息
     * @param integer $hotelId 客栈ID
     * @param integer $name    等级名称
     * @return type
     */
    public function getInfoByUserId($userId) {
        $option = [
            'vipuser_id'=> $userId,
        ];
        $data = $this->where($option)->find();

        return empty($data) ? array() : $data;
    }

    /**
     *
     * 添加用户数据
     * @modify zhangzhen 2017-02-15
     * @param integer $vipUserId       客栈ID
     * @param string  $gender          性别
     * @param string  $birthday        生日
     * @param string  $nation          民族
     * @param string  $address         地址
     * @param string  $wechat          微信
     * @param string  $remark          备注
     * @return type
     */
    public function addInfo($vipUserId, $gender = null, $birthday = null, $nation = null, $address = null, $wechat = null, $remark = null) {
        $data = [
            'vipuser_id' => $vipUserId,
        ];
        if(!empty($gender)){
            $data['gender'] = $gender;
        }
        if(!empty($birthday)){
            $data['birthday'] = $birthday;
        }
        if(!empty($nation)){
            $data['nation'] = $nation;
        }
        if(!empty($address)){
            $data['address'] = $address;
        }
        if(!empty($wechat)){
            $data['wechat'] = $wechat;
        }
        if(!empty($remark)){
            $data['remark'] = $remark;
        }
        return $this->add($data);
    }

    /**
     * 更新用户数据
     * @modify zhangzhen 2017-02-18
     * @param integer $vipUserId      会员id
     * @param integer $vipInfoId      等级ID
     * @param string  $name           名字
     * @param string  $mobile         手机号码
     * @param string  $identity       身份证
     * @return type
     */
    public function saveInfo($vipUserId, $gender = null, $birthday = null, $nation = null, $address = null, $wechat = null, $remark = null) {
        $option = [];
        if (is_array($vipUserId)) {
            $vipUserId = implode(',', $vipUserId);
        }
        if (strpos($vipUserId, ',') !== false) {
            $option['vipuser_id'] = array('in', $vipUserId);
        } else {
            $option['vipuser_id'] = $vipUserId;
        }

        if(!is_null($gender)){
            $data['gender'] = $gender;
        }
        if(!empty($birthday)){
            $data['birthday'] = $birthday;
        }
        if(!empty($nation)){
            $data['nation'] = $nation;
        }
        if(!empty($address)){
            $data['address'] = $address;
        }
        if(!empty($wechat)){
            $data['wechat'] = $wechat;
        }
        if(!empty($remark)){
            $data['remark'] = $remark;
        }
        $result = $this->where($option)->save($data);
        return $result === false ? false : true;
    }

}
